fix: Critical routing bug - param nodes with multiple static children (v0.3.3)#4
Merged
fix: Critical routing bug - param nodes with multiple static children (v0.3.3)#4
Conversation
Fixes critical routing bug where routes with :param followed by multiple static segments would panic. For example, registering both /users/:id/activate and /users/:id/deactivate caused panic at line 147 in tree.go. Root cause: Common prefix check compared /deactivate with :id and found no match, causing "internal error: no common prefix" panic. Solution: Implemented httprouter's pattern of creating empty placeholder child nodes after param nodes and bypassing common prefix check when inserting children of param nodes. Changes: - internal/radix/tree.go (lines 118-138): Added special case handling for param node children following httprouter's approach - internal/radix/tree_param_static_test.go: Added 6 comprehensive test cases covering all param+static patterns Testing: - All 650+ tests passing with race detector (WSL2 Gentoo) - Coverage maintained: 88.4% internal/radix, 91.7% overall - Linter: 0 issues (golangci-lint clean) - Performance: No regressions (256 ns/op static, 326 ns/op parametric) Studied httprouter implementation (tree.go lines 217-270, 180-184) for production-quality reference solution.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes critical routing bug where routes with
:paramfollowed by multiple static segments would panic.Problem
Routes like
/users/:id/activate+/users/:id/deactivatecaused panic at line 147 intree.go:Root Cause
Common prefix check compared
/deactivatewith:idand found no match.Solution
Implemented httprouter's production-quality pattern:
n.children = []*node{child}) to allow empty path placeholdersChanges
internal/radix/tree.go(lines 118-138): Added special case handling for param node childreninternal/radix/tree_param_static_test.go(new file): 6 comprehensive test casesCHANGELOG.md: Documented v0.3.3 releaseTesting
✅ All 650+ tests passing with race detector (WSL2 Gentoo)
✅ Coverage: 88.4% internal/radix, 91.7% overall (maintained)
✅ Linter: 0 issues (golangci-lint clean)
✅ Performance: No regressions (256 ns/op static, 326 ns/op parametric)
✅ CI: GREEN
Test Cases Added
TestTree_ParamWithMultipleStaticChildren- Original bug caseTestTree_NestedParams- Nested params (/users/:user_id/posts/:post_id/comments)TestTree_AlternatingParamStatic- Alternating pattern (/a/:b/c/:d/e)TestTree_ParamWithLongStaticTail- Long static tails (/:a/b/c/d/e/f/g)TestTree_ConsecutiveParams- Consecutive params (/:a/:b/:c/d)TestTree_StaticVsParam- Static priority over paramsReference
Studied httprouter implementation (
tree.golines 217-270, 180-184) for production-quality reference solution.Checklist